package com.example.administrator.q1;

import android.content.Context;
import android.support.v4.view.PagerAdapter;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

import java.util.List;

/**
 * Created by Administrator on 2017/8/30.
 */

public class MyAdapter extends BaseAdapter {

    // 将list中的内容适配到context上去
    Context context;
    List<String> list;
    LayoutInflater inflater;

    public MyAdapter(Context context, List<String> m_list) {
        this.context = context;
        this.list = m_list;
        inflater = LayoutInflater.from(context);
    }

    @Override
    public int getCount() {
        // 计算当前数量
        return list == null ? 0 : list.size();
    }

    @Override
    public Object getItem(int i) {
        // 获得i位置的内容
        return list.get(i);
    }

    @Override
    public long getItemId(int i) {
        // 获得位置
        return i;
    }

    @Override
    public View getView(int i, View view, ViewGroup viewGroup) {
        // 将获得的内容以制定的方式来显示
        // 就当前的来说，是将listAdapte成VIew, 即返回View
        ViewHolder viewHolder;
        if (view == null) {
            // 加载布局为一个视图
            view = inflater.inflate(R.layout.list_string_to_view, null);
            // 将要加载的msg加载
            viewHolder = new ViewHolder();
            // 在View中查找并返回view
            viewHolder.textView = (TextView)view.findViewById(R.id.textView_showList);
            // 设置view的内容
            viewHolder.textView.setText(list.get(i));
            view.setTag(viewHolder);
        } else {
            viewHolder = (ViewHolder)view.getTag();
        }
        return view;
    }

    class ViewHolder {
        TextView textView;
    }
}
